<?php
class Warehouse_AreaController extends Zend_Controller_Action
{
	/**
	* 仓库分区列表  
	*
	*/
	private $_s;
	private $_h;		//房间
	private $_shelf;	//货架	
	public function preDispatch(){
		$this->_s=new Warehouse_Service_Area();
		$this->_h=new Warehouse_Service_House();
		$this->_shelf=new Warehouse_Service_Shelf();		
	}	
	
	public function indexAction(){
		$this->view->typeText=Warehouse_Service_Area::$typeText;
		$this->view->OptionChoose=$this->_s->getProductOptionChoose(1);
		echo Cff::renderTpl('warehouse/area/index.tpl',"layout");
	}	

	public function listAction(){
	
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize');
		$wa_code=$this->_request->getParam('wa_code','');
		$intpage = ($intpage<1)?1:$intpage; 
		$condition=array(
			'wa_code'=>$wa_code,
		);
		$count=$this->_s->getByCondition($condition,'count(*)','',0,0);
		$data=array(
			'count'=>0,
			'ask'=>0
			);
		if($count>0){
			$Rows=$this->_s->getByCondition($condition,'*','wa_id desc',$pageSize,$intpage);
			foreach($Rows as $key => $val){
				$Rows[$key]['wa_type']=Warehouse_Service_Area::$typeText[$val['wa_type']];
			}
			$data=array(
				'count'=>$count,
				'result'=>$Rows,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));

	}	
	
	/**
	*仓库分区添加、修改
	*@param row=array()
	*@return json
	*/		
	public function createAction(){
		$wa_status=$this->_request->getPost('wa_status');
		$wa_name=$this->_request->getPost('wa_name');
		$wa_id=$this->_request->getPost('wa_id','');
		$wa_code=$this->_request->getPost('wa_code','');
		$warehouse_id=$this->_request->getPost('warehouse_id','');
		$wa_type=trim($this->_request->getPost('wa_type',''));
		$poc_id=trim($this->_request->getPost('poc_id',''));
		$check_poc=trim($this->_request->getPost('check_poc',''));
		if($check_poc==0){$poc_id=0;}
		$error=array();
		$date=date("Y-m-d H:i:s");
		$message['ask']=0;
		$data=0;
		$rows=0;
		$row=array(
			'wa_status'=>$wa_status,
			'wa_name'=>$wa_name,
			'wa_create_time'=>$date,
			'wa_code'=>$wa_code,
			'warehouse_id'=>$warehouse_id,
			'wa_type'=>$wa_type,
			'poc_id'=>$poc_id,
		);
		$error = $this->_s->validator($row);//表单数据验证
        if(!empty($error)){
			$return=array(
				'ask'=>0,
				'error'=>$error,
			);
            die(Zend_Json::encode($return));
        }
		if(!empty($wa_id)){
			$inforows=$this->_s->getById($wa_id);
			unset($row['wa_create_time']);
			unset($row['warehouse_id']);
			unset($row['wa_code']);
			$row['wa_last_update']=$date;
			if(!empty($inforows)){
				//判断区域对应目录与产品是否符合
				if($poc_id!=''){
					$shelfRow=Warehouse_Service_Products::getByAreaValidateProductCondition(array('wa_code'=>$inforows['wa_code'],'select_poc_id'=>$poc_id),'*');
					if($shelfRow['ask']==0){
						die(Zend_Json::encode(array('ask'=>0,'error'=>array('此区域存在目录不符的库存,不允许修改!',$shelfRow['message']))));
					}
				}
				if($inforows['wa_name']==$wa_name && $inforows['wa_status']==$wa_status && $inforows['wa_type']==$wa_type && $inforows['poc_id']==$poc_id ){
					$data=3;	//没有修改任何资料
				}else{
					$rows=$this->_s->update($row,$wa_id);
					if($inforows['wa_status']!=$wa_status){
						//$Row=$this->_h->updateStatus($inforows['wa_code'],$wa_status);//级联修改house
						$Row=$this->_shelf->updateStatus($inforows['wa_code'],$wa_status);//级联修改shelf
					}
					$data=2;   //成功修改
				}
			}
		}else{
			$exist=$this->_s->getByfield('wa_code',$wa_code);//是否存在
			if(!empty($exist)){
				$error[]="添加失败 ".$wa_code." 分区已存在!";
				$return=array(
					'ask'=>0,
					'error'=>$error,
				);
				die(Zend_Json::encode($return));
			}
			$rows=$this->_s->create($row);
			if($rows>0){
				$data=1; //添加成功
			}	
		}
		$message['ask']=$data;
		die(Zend_Json::encode($message));
	}


	public function infoAction(){
		$wa_id=$this->_request->getParam('wa_id');
		$return=array(
			'ask'=>0
		);
		if(!empty($wa_id)){
			$rows=$this->_s->getById($wa_id);
			$return=array(
				'ask'=>1,
				'result'=>$rows
			);			
		}
		die(Zend_Json::encode($return));
	}	
	
	public function deleteAction(){
		$t_id=$this->_request->getPost('wa_id');
		$rows=0;
        try{
			if(!empty($t_id)){
				$rows=$this->_s->delete($t_id);
			}
			if($rows>0){
				$massage['ask']=1;
			}else{
				$massage['ask']=0;
			}
        }catch(Zend_Exception $e){
			$massage['ask']=0;
			$massage['result']=$e->getMessage();
        }		
		die(Zend_Json::encode($massage));
	}
	
	/**
	*区域对应货架列表
	*@return json
	*/		
	public function getlistAction(){
		$wa_code=trim($this->_request->getParam('wa_code',''));
		$wa_name=trim($this->_request->getParam('wa_name',''));
		if($this->_request->isPost()){
			$intpage=$this->_request->getParam('page',1);
			$intpage=($intpage<1)?1:$intpage;
			$pageSize=$this->_request->getParam('pageSize',60);
			$return=array(
				'ask'=>0
				);
			$total = $this->_shelf->getByCondition(array('wa_code'=>$wa_code),"count(*)");
			$return = array("ask" =>0,"total" => $total);
			if($total>0){
				$Rows=$this->_shelf->getByCondition(array('wa_code'=>$wa_code),'*',array('ws_code ASC','wa_code'),$pageSize,$intpage);	
				if(!empty($Rows)){
					$list=array();
					foreach($Rows as $key => $val){
						$list[$key]['ws_code']=$val['ws_code'];
					}
					$return=array(
						'ask'=>1,
						'total'=>$total,
						'result'=>$list,
					);
				}
			}	
			die(Zend_Json::encode($return));
		}else{
			$this->view->wa_code=$wa_code;
			$this->view->wa_name=$wa_name;
			echo Cff::renderTpl('warehouse/shelf/show-shelf.tpl',"layoutheader");
		}		
	}	
	
	/**
	*仓库分区列表
	*house,shelf 等页面引用加载 
	*@return json
	*/	
	public function selectAction(){
		$data=array('ask'=>0);
		$result=$this->_s->getByCondition('','*','wa_code',0,0);  //仓库
		if(!empty($result)){
			$data=array(
				'ask'=>1,
				'result'=>$result,
			);
		}
		die(Zend_Json::encode($data));
	}	
		
}